<template>
  <div>
    <!-- 添加用户 -->
    <el-button type="primary" @click="dialogFormVisible = true"
      >点击添加用户</el-button
    >
    <el-dialog title="添加用户" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="用户名">
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <!-- <el-form-item label="昵称">
          <el-input v-model="form.role_name" autocomplete="off"></el-input>
        </el-form-item> -->
        <el-form-item label="手机号">
          <el-input v-model="form.mobile" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="form.email" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="zhuce">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 修改用户信息 -->
    <el-dialog title="修改用户信息" :visible.sync="xiugaiForm">
      <el-form :model="updataform">
        <el-form-item label="用户名">
          <el-input v-model="updataform.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="updataform.mobile" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="updataform.email" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="xiugaiForm = false">取 消</el-button>
        <el-button type="primary" @click="quedingxiugai">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 修改权限 -->
    <el-dialog title="修改用户权限" :visible.sync="xiugaiquanxian">
      <el-form :model="quanxianform">
        <el-form-item label="用户名">
          <el-input
            v-model="quanxianform.username"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <!-- 展示所有角色 -->
        <el-select v-model="roleid" placeholder="请选择">
          <el-option
            v-for="item in rolelist"
            :key="item.id"
            :label="item.roleName"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="xiugaiquanxian = false">取 消</el-button>
        <el-button type="primary" @click="quedingquanxian">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 内容 -->
    <el-table :data="tableData" stripe>
      <el-table-column prop="username" label="用户名"> </el-table-column>
      <el-table-column prop="role_name" label="呢称"> </el-table-column>
      <el-table-column prop="mobile" label="手机号"> </el-table-column>
      <el-table-column prop="email" label="邮箱"> </el-table-column>
      <el-table-column prop="mg_state" label="状态">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.mg_state"
            active-color="#13ce66"
            inactive-color="#ff4949"
            @change="change(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            title="修改用户信息"
            circle
            @click="xiugai(scope.row)"
          ></el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            title="删除用户信息"
            circle
            @click="del(scope.row)"
          ></el-button>
          <el-button
            type="warning"
            icon="el-icon-star-off"
            circle
            @click="quanxian(scope.row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页按钮 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[2, 3, 5, 10]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>
<script>
export default {
  name: "VueUser",

  data() {
    return {
      tableData: [],
      total: 0,
      pageSize: 2,
      pageNum: 1,

      dialogTableVisible: false,
      dialogFormVisible: false,
      xiugaiForm: false,
      xiugaiquanxian: false,
      //修改的弹窗
      form: {
        username: "",
        mobile: "",
        email: "",
        password: "123456",
      },
      updataform: {},
      //权限的弹窗
      quanxianform: {},
      //循环的权限数组
      rolelist: [],
      roleid: "",

      // rolena: "",
    };
  },

  methods: {
    // 确定修改权限
    async quedingquanxian() {
      // console.log(this.roleid);
      // console.log(this.quanxianform.id);
      var { data } = await this.$axios.put(
        `/users/${this.quanxianform.id}/role`,
        {
          rid: this.roleid,
        }
      );
      // console.log(data);
      if (data.meta.status == 200) {
        this.$message({
          message: data.meta.msg,
          type: "success",
        });
        this.getinfo(this.pageNum, this.pageSize);
      }
    },
    //修改权限界面
    async quanxian(updataform) {
      this.xiugaiquanxian = true;
      this.quanxianform = updataform;
      var { data } = await this.$axios.get("/roles");
      // console.log(data);
      this.rolelist = data.data;
      // this.rolena = updataform.role_name;
    },
    //删除用户
    async del(updataform) {
      var { data } = await this.$axios.delete("/users/" + updataform.id);
      console.log(data);
      if (data.meta.status == 200) {
        this.$message({
          message: data.meta.msg,
          type: "success",
        });
        this.getinfo(this.pageNum, this.pageSize);
      }
    },

    //修改信息
    xiugai(updata) {
      console.log(updata);
      this.xiugaiForm = true;
      this.updataform = updata;
    },
    //确定修改
    async quedingxiugai() {
      console.log(this.updataform);
      // let obj = {
      //   id: this.updataform.id,
      //   email: this.updataform.email,
      //   mobile: this.updataform.mobile,
      // };
      var { data } = await this.$axios.put(
        "/users/" + this.updataform.id,
        this.updataform
      );
      // console.log(data);
      if (data.meta.status == 200) {
        this.$message({
          message: data.meta.msg,
          type: "success",
        });
      } else {
        this.$message.error("修改失败" + data.meta.msg.sqlMessage);
      }
    },

    //注册新用户
    async zhuce() {
      console.log(1);
      console.log(this.form);
      var { data } = await this.$axios.post("/users", this.form);
      // console.log(data);
      if (data.meta.status == 201) {
        this.$message({
          message: data.meta.msg,
          type: "success",
        });
        this.getinfo(this.pageNum, this.pageSize);
      }
    },
    //改变状态
    async change(a) {
      var { data } = await this.$axios.put(
        `/users/${a.id}/state/${a.mg_state}`
      );
      console.log(data);
      if (data.meta.status == 200) {
        this.$message({
          message: data.meta.msg,
          type: "success",
        });
      }
    },

    async getinfo(pagenum = 1, pagesize = 3) {
      var { data } = await this.$axios.get(
        `/users?pagenum=${pagenum}&pagesize=${pagesize}`
      );
      if (data.meta.status == 200) {
        this.tableData = data.data.users;
        console.log(data.data);
        this.pageNum = data.data.pageNum;
        this.total = data.data.total;
      }
    },
    async handleSizeChange(pagesize) {
      this.pageSize = pagesize;
      this.getinfo(1, pagesize);
    },

    async handleCurrentChange(pagenum) {
      this.getinfo(pagenum, this.pageSize);
    },
  },

  async mounted() {
    // var { data } = await this.$axios.get(
    //   "/users?pagenum=1&pagesize=" + this.pageSize
    // );
    // if (data.meta.status == 200) {
    //   this.tableData = data.data.users;
    //   console.log(data.data);
    //   this.total = data.data.total;
    // }
    this.getinfo(1, 2);
  },
};
</script>

<style lang="css">
</style>